from django.shortcuts import render, get_object_or_404
from .models import Post


def post_list(request):
    posts = Post.published.all()  # 使用了自定义管理器published，列出所有已发布的帖子
    return render(request,
                  'blog/post/list.html',
                  {'posts': posts})


def post_detail(request, year, month, day, post):  # 这里可以通过动态url连接 获取对应的帖子信息
    post = get_object_or_404(Post, slug=post,
                             status='published',
                             publish__year=year,
                             publish__month=month,
                             publish__day=day)
    return render(request,
                  'blog/post/detail.html',
                  {'post': post})


# 看看分页的方法
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def post_list(request):
    object_list = Post.published.all()
    paginator = Paginator(object_list, 3) # 三条记录一页
    page = request.GET.get('page') # 获取到页面传递的页面数
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # 如果页面不是整数返回第一页
        posts = paginator.page(1)
    except EmptyPage:
        # 如果页面数超过宗卓 就返回最大页
        posts = paginator.page(paginator.num_pages)
    return render(request,
                  'blog/post/list.html',
                  {'page': page,
                   'posts': posts})



from blog.forms import EmailPostForm
def post_share(request, post_id):
    # 通过id查询出推荐的信息
    post = get_object_or_404(Post, id=post_id, status='published')
    cd = None  # cleaned_data 缩写 这里生命一下
    if request.method == 'POST':
        # EmailPostForm类通过reuqest.POST实例化
        form = EmailPostForm(request.POST)
        # 验证成功后获取信息
        if form.is_valid():
            cd = form.cleaned_data
    else:
        # 如果不是post请求 返回form表单
        form = EmailPostForm()
    return render(request, 'blog/post/share.html', {'post': post,
                                                        'form': form,  'cd':cd})

from django.core.mail import send_mail # 导入send_mail方法

def post_share(request, post_id):
    # 通过id和发布状态获取post对象
    post = get_object_or_404(Post, id=post_id, status='published')
    # 邮件发送状态标识
    sent = False
    if request.method == 'POST':
        # EmailPostForm类通过reuqest.POST实例化
        form = EmailPostForm(request.POST)
        if form.is_valid():
            # Form fields passed validation
            cd = form.cleaned_data
            print(cd)
            getmail = cd['to']
            # 获取博文路径地址
            post_url = request.build_absolute_uri(   # 这个方法去看下 #1.11换成build_absolute_uri() 1.8还是build_absolute_url()
                                    post.get_absolute_url())
            subject = '{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title)
            message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments'])
            send_mail('xxxx', 'xxx','2936877795@qq.com', ['wuyang_229@126.com'])
            #send_mail('Django mail', 'This e-mail was sent with Django.','2936877795@qq.com', ['2936877795@qq.com'], fail_silently=False)
            sent = True
    else:
        form = EmailPostForm()

    return render(request, 'blog/post/share.html', {'post': post,
                                                    'form': form,
                                                    'sent': sent})
